#include <vector>
#include <algorithm>
using namespace std;
/*
 * @lc app=leetcode.cn id=123 lang=cpp
 *
 * [123] 买卖股票的最佳时机 III
 */

// @lc code=start
class Solution {
public:
    // 思路很巧妙
    int maxProfit(vector<int>& prices) {
        int o0s1 = 0, o1s1 = -prices[0], o0s2 = 0, o1s0 = -prices[0];
        for (int i = 0; i < prices.size(); i++) {
            o0s2 = max(o0s2, o1s1 + prices[i]);
            o1s1 = max(o1s1, o0s1 - prices[i]);
            o0s1 = max(o0s1, o1s0 + prices[i]);
            o1s0 = max(o1s0, -prices[i]);
        }
        return o0s2;
    }
};
// @lc code=end

